﻿using System;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.SessionState;
using System.Data;
using DBA.Common;
using DBA.Data;
using DBA.Data.ADO;
using DBA.Model;
using DBA.Web.UI;
using DBA.Bll;


namespace DBA.Web.PaperManage
{
    /// <summary>
    /// UploadPaperExcel1 的摘要说明
    /// </summary>
    public class UploadPaperExcel1 : IHttpHandler, IRequiresSessionState
    {

        private const int UploadFileLimit = 2;//上传文件数量限制
        private string _msg = "文件导入成功！";//返回信息
        private int GetUserType(string UserName)
        {
            if (VerificationHelper.IsNullOrEmpty<string>(UserName)) //如果为空，那么返回-2
            {
                return -2;
            }
            DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
            dal.ManagerParameter.ClearParameter();
            dal.ManagerParameter.AddNewInParameter("Name", UserName);
            int teacherCount = ConvertHelper.ToInt32(dal.ExecuteScalar("select count(1) from EmployeeInfo where Name = @Name"));
            if (teacherCount > 0) //如果在教师中找到此人，那么返回1
            {
                return 1;
            }
            int studentCount = ConvertHelper.ToInt32(dal.ExecuteScalar("select count(1) from Student where Name = @Name"));
            if (studentCount > 0) //如果在学生中找到此人，那么返回2
            {
                return 2;
            }
            return -1; //如果找不到，那么返回-1
        }
        public void ProcessRequest(HttpContext context)
        {
            int iTotal = context.Request.Files.Count;
            if (iTotal == 0)
            {
                _msg = "没有数据";
            }
            else
            {
                int iCount = 0;
                string fileFolder = "upload/" + DateTime.Now.Year + "year/" + DateTime.Now.Month + "month/";
                string savaFileName = "";
                string randomstring = "";

                for (int i = 0; i < iTotal; i++)
                {
                    HttpPostedFile file = context.Request.Files[i];
                    if (!Directory.Exists(UtilsHelper.GetMapPath(UtilsHelper.ServerUrl + fileFolder)))
                    {
                        Directory.CreateDirectory(UtilsHelper.GetMapPath(UtilsHelper.ServerUrl + fileFolder));
                    }
                    if (file.ContentLength > 0 || !string.IsNullOrEmpty(file.FileName))
                    {
                        randomstring = new Random().Next(50000).ToString();


                        savaFileName = UtilsHelper.GetMapPath(UtilsHelper.ServerUrl + fileFolder) + randomstring + file.FileName.Substring(file.FileName.LastIndexOf('\\') + 1).Replace(" ", "");

                        try
                        {
                            file.SaveAs(savaFileName);
                            DataTable dt = NPOIHelper.ImportDataTable(savaFileName);
                            for (int iRowsCount = 0; iRowsCount < dt.Rows.Count; iRowsCount++)
                            {
                                DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
                                PaperSum papersum = new PaperSum();
                                papersum.Ps_PaperName = dt.Rows[iRowsCount][0].ToString();
                                papersum.Ps_PublicationName = dt.Rows[iRowsCount][1].ToString();
                                papersum.Ps_EnglishName = dt.Rows[iRowsCount][2].ToString();
                                papersum.Ps_Type = (short)(dt.Rows[iRowsCount][3].ToString() == "期刊论文" ? 2 : 1);
                                papersum.Ps_Class = dt.Rows[iRowsCount][4].ToString();
                                papersum.Ps_Issue = dt.Rows[iRowsCount][5].ToString();
                                papersum.Ps_addressPerson = dt.Rows[iRowsCount][6].ToString();
                                papersum.Ps_date = dt.Rows[iRowsCount][7].ToString();
                                papersum.Ps_Sci = dt.Rows[iRowsCount][8].ToString();
                                papersum.Ps_Weight = ConvertHelper.ToDecimal(dt.Rows[iRowsCount][9]);
                                papersum.Ps_Person1 = dt.Rows[iRowsCount][10].ToString();
                                papersum.Ps_Person2 = dt.Rows[iRowsCount][11].ToString();
                                papersum.Ps_Person3 = dt.Rows[iRowsCount][12].ToString();
                                papersum.Ps_Person4 = dt.Rows[iRowsCount][13].ToString();
                                papersum.Ps_Demo = dt.Rows[iRowsCount][14].ToString();
                                papersum.Ps_AttachName = "";
                                papersum.Person1Is = (short)GetUserType(papersum.Ps_Person1);
                                papersum.Person2Is = (short)GetUserType(papersum.Ps_Person2);
                                papersum.Person3Is = (short)GetUserType(papersum.Ps_Person3);
                                papersum.Person4Is = (short)GetUserType(papersum.Ps_Person4);
                                dal.ModifyData.Insert<PaperSum>(papersum);
                                //dal.ManagerParameter.ClearParameter();
                                //dal.ManagerParameter.AddNewInParameter("Name", dt.Rows[iRowsCount][0], DbType.String, 20);
                                //dal.ManagerParameter.AddNewInParameter("CourseName", dt.Rows[iRowsCount][1], DbType.String, 100);
                                //dal.ManagerParameter.AddNewInParameter("Period", ConvertHelper.ToInt32(dt.Rows[iRowsCount][2]), DbType.Int32, 4);
                                //dal.ManagerParameter.AddNewInParameter("Grade", ConvertHelper.ToInt32(dt.Rows[iRowsCount][3]), DbType.Int32, 4);
                                //dal.ManagerParameter.AddNewOutParameter("reValue", DbType.Int32);
                                //dal.ExecuteStore("Proc_BatchAddTeacherCourse");
                                //string m = dal.ManagerParameter.GetParameter("reValue");
                            }
                        }
                        catch
                        {
                            _msg = "导入中出现错误，请重新尝试!";
                            break;
                        }
                        //这里可以根据实际设置其他限制
                        if (++iCount > UploadFileLimit)
                        {
                            _msg = "超过上传限制：" + UploadFileLimit;
                            break;
                        }
                    }
                }
            }
            context.Response.Write(_msg);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}